#!/bin/bash
##
#   Read saved BunsenLabs Conky session file(s) and start the conkys
#
#   Written by damo <damo@bunsenlabs.org> for BunsenLabs Linux, April 2015
#
#   To start the default conky session at login, add the following line
#   to config/openbox/autostart:
#
#      (sleep 2s && am-conky-session --autostart) &
#
########################################################################
CONKYPATH="$HOME/.config/conky"
SESSIONFILE="$CONKYPATH/conky-sessionfile"
USAGE="\vUSAGE:\tam-conky-session [OPTION(S)]...FILES
\n\tWith no command argument, the script uses the default
\t\"\$CONKYPATH/conky-session\" sessionfile
\vOPTIONS:\n\t--default\t: specify default sessionfile
\t--autostart\t: no \"kill conky\" option asked for
\tpath/to/sessionfile1  /path/to/sessionfile2 etc
\vEXAMPLES:
\tRun specified sessionfile at login:
\t\"am-conky-session --autostart /path/to/sessionfile\"
\v\tRun default sessionfile, without killing running conkys:
\t\"am-conky-session --autostart\"
\v\tRun several conky sessionfiles (option to kill conkys first):
\t\"am-conky-session --default sessionfile1 sessionfile2 etc\""

### DIALOG VARIABLES
DLG="yad --center --undecorated --borders=20 "
TITLE="ArcoLinux Conky Session"
WINICON="--window-icon=distributor-logo-arcolinux"
OK="--button=yad-ok:0"
CANCEL="--button=yad-cancel:1"

########################################################################

findArgs(){     # get command args (switches & sessionfile paths)
    i=0
    for arg in "$@";do
        if [[ $arg = "--default" ]]; then
            arg="$SESSIONFILE"
        fi
        if [[ $arg = "--autostart" ]]; then
            NOKILL=1    # run from autostart, so don't ask to kill conkys
        fi
        if [[ -f  $arg ]]; then # sessionfile exists
            rawArr[$i]="$arg"       # add sessionfiles to array
            i=$(($i+1))
        fi
    done
    # check if sessionfiles were passed to am-conky-session
    if (( ${#rawArr[@]} != 0 )); then
        # remove duplicate args
        sessArr=(`printf "%s\n" "${rawArr[@]}" | sort -u`)
        if (( $NOKILL == 0 )); then
            killConkys
        fi
        for SESSION in "${sessArr[@]}";do # run the conkys in the sessionfiles
            source "$SESSION"
        done
    else    # --autostart used, but no sessionfiles passed to am-conkyzen
        if [[ -f $SESSIONFILE ]] && (( $NOKILL == 1 )); then
            source "$SESSIONFILE"   # use conky-sessionfile
        else
            echo -e "ERROR: sessionfile \"$SESSIONFILE\" not found. Using default" >&2
            conky -c $HOME/.conkyrc     # run default conky
            exit 0
        fi
    fi
}

killConkys(){
    if pidof conky &>/dev/null; then
        #MSG="Kill running conkys first?"
        #$DLG $WINICON --title="$TITLE" --text="$MSG"
        #if (( $? == 0 )); then            # kill all conkys
            killall conky && sleep 0.2s
        #fi
    fi
}

NOKILL=0
if (( $# == 0 )); then    # if no args, then run default sessionfile
    killConkys
    if [[ ! -f "$SESSIONFILE" ]]; then     # run default conky
        echo -e "ERROR: sessionfile \"$SESSIONFILE\" not found. Using default" >&2
        conky -c $HOME/.conkyrc
        exit 0
    else
        source "$SESSIONFILE"
    fi
elif [[ $1 = "-h" ]] || [[ $1 = "--help" ]]; then
    echo -e "$USAGE"
    exit 0
else
    findArgs $@    # get the sessionfile paths from the command args
fi

exit 0
